home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / MSC2BC.ARJ / MFARHEAP.C < prev    next >
C/C++ Source or Header  |  1992-01-22  |  7KB  |  207 lines

  1. /* MFARHEAP.C - Far heap example #1.
  2.    This program illustrates Microsoft C6.0a far heap memory
  3.    management using _fheapset, _fheapchk, _fmalloc, _fmsize,
  4.    _fmemset, _ffree, and _fheapwalk. It allocates, mainpulates,
  5.    and deallocates two sets of far heap memory blocks, and shows
  6.    some of the details about how far heap management works.
  7.  
  8.    Copyright (c) 1991 Borland International. All rights reserved.
  9.  
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <conio.h>
  14. #include <malloc.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. /* Note 1 - No #defines here since this is a Microsoft C program */
  18.  
  19. /* Function prototypes and manifest constants */
  20. void heapdump( char fill );
  21. void heap_status( int status );
  22.  
  23. #define IMAX 10 /* number of heap blocks to allocate */
  24. void main()
  25. {
  26.    void _far *p[10];
  27.    /* Note 2  - Just the heap pointer array p is here. */
  28.    int i, n_alloc;
  29.  
  30.     /* Fill all current heap blocks with known data. */
  31.     heap_status( _fheapset( 254 ) );
  32.  
  33.     printf ("Free heap has been initialized.\n");
  34.  
  35.     /* Check heap consistency and dump it before doing anything else. */
  36.     heapdump( (char)254 );
  37.  
  38.     /* Check heap status. */
  39.     heap_status( _fheapchk() );
  40.     printf ("Just before allocating blocks.\n");
  41.  
  42.     /* Now allocate IMAX fixed blocks of memory to affect the far heap,
  43.        and fill them with ones. */
  44.     for( i = 0; i < IMAX; i++ )
  45.     {
  46.     /* Note 3 - Use the heap size directly here. */
  47.     /* Note 4 - Microsoft C even allows you to allocate a heap block
  48.        that is zero bytes in size.
  49.     */
  50.     if( (p[i] = _fmalloc( (size_t)i*10 )) != NULL )
  51.       printf( "Allocated %u bytes on far heap at %Fp\n",
  52.           _fmsize(p[i]), p[i] );
  53.     else
  54.       break;
  55.     }
  56.     n_alloc = i-1;  /* Index of last block successfully allocated */
  57.     /* Note 5 - With Microsoft C6.0, if there is not enough space
  58.        available on the free heap to satisfy a given call, the
  59.        heap manager will grow the heap when possible.  The
  60.        conditions that allow that are: free memory is available
  61.        from the operating system, and the heap will not grow
  62.        beyond the maximum size permissible for the type of heap.
  63.  
  64.        As a result, you may see more free memory available after
  65.        space is allocated than before.
  66.     */
  67.  
  68.     printf ("Just after allocating heap blocks.\n" );
  69.     /* Fill all free heap blocks with known data. */
  70.     heap_status( _fheapset( 254 ) );
  71.     printf ("Free heap has been initialized again.\n" );
  72.  
  73.     /* Fill all allocated heap blocks with different data. */
  74.     for( i = 0; i < n_alloc; i++ )
  75.     _fmemset( p[i], 0xFF, _fmsize( p[i] ) );
  76.     printf ("All allocated heap blocks are now filled with 0xFF.\n" );
  77.  
  78.     /* Check heap consistency again. */
  79.     heapdump( (char)254 );
  80.  
  81.     /* Now free up the heap blocks. */
  82.     for( i=n_alloc; i >= 0; i-- )
  83.     {
  84.     printf( "Deallocating %u bytes from far heap at %Fp\n",
  85.         _fmsize( p[i] ), p[i] );
  86.     _ffree( p[i] );
  87.     }
  88.  
  89.     /* Check heap consistency after releasing memory. */
  90.     heapdump( (char)254 );
  91.  
  92.     /* Now AGAIN allocate IMAX fixed blocks of memory to
  93.        affect the far heap, and fill them with ones. This
  94.        will see whether free heap gets concatenated together
  95.        to satisfy a given request.
  96.     */
  97.     printf ("Just before allocating blocks second time.\n");
  98.     for( i = 0; i < IMAX; i++ )
  99.     {
  100.     if( (p[i] = _fmalloc( (size_t)i*10 )) != NULL )  /* farmalloc */
  101.       printf( "Allocated %u bytes on far heap at %Fp\n",
  102.           _fmsize(p[i]), p[i] );
  103.     else
  104.       break;
  105.     }
  106.     n_alloc = i-1;  /* Index of last block successfully allocated */
  107.     printf ("Just after allocating heap blocks second time.\n" );
  108.     /* Fill all free heap blocks with known data. */
  109.     heap_status( _fheapset( 254 ) );
  110.     printf ("Free heap has been initialized again.\n" );
  111.  
  112.     /* Fill all allocated heap blocks with different data. */
  113.     for( i = 0; i < n_alloc; i++ )
  114.     _fmemset( p[i], 0, _fmsize( p[i] ) );
  115.     printf ("All allocated heap blocks are now filled with 0.\n" );
  116.  
  117.     /* Check heap consistency again. */
  118.     heapdump( (char)254 );
  119.  
  120.     /* Now free up the heap blocks. */
  121.     for( i=n_alloc; i >= 0; i-- )
  122.     {
  123.     printf( "Deallocating %u bytes from far heap at %Fp\n",
  124.         _fmsize( p[i] ), p[i] );
  125.     _ffree( p[i] );
  126.     }
  127.  
  128.     /* Final check of heap consistency. */
  129.     heapdump( (char)254 );
  130.  
  131.     exit(0);
  132. }
  133.  
  134. void heapdump( char fill )
  135. /* heapdump walks through all heap entries, checks consistency
  136.    of free blocks, and displays information about each heap block.
  137. */
  138. {
  139.     struct _heapinfo hi;
  140.     int hstate, i;
  141.     char _far *p;
  142.  
  143.     printf("\n-- Current Heap Blocks --\nStatus    Size   Address\n");
  144.     hi._pentry = NULL;      /* Initialize to get first heap entry */
  145.     while( ( hstate = _fheapwalk( &hi )) == _HEAPOK )
  146.     {
  147.     /* Note 6 - If the _useflag field in the _heapinfo structure
  148.        = _USEDENTRY, then the heap block is "used", not "free".
  149.        The _size field is an unsigned integer, and it reports exactly
  150.        the number of bytes to which the program has access.
  151.      */
  152.     printf( "%s  %10u %Fp ",
  153.         hi._useflag == _USEDENTRY ? "USED" : "FREE",
  154.         hi._size,
  155.         hi._pentry );
  156.  
  157.     /* Note 7 - For free heap entries, check each byte to see that it
  158.        contains the requested fill character.
  159.      */
  160.     if( hi._useflag != _USEDENTRY )
  161.     {
  162.         for( p = (char _far *)hi._pentry, i = 0; i < hi._size; p++,i++ )
  163.         if( (char)*p != fill )
  164.             break;
  165.         if( i == hi._size )
  166.         printf( "Not changed\n" );
  167.         else
  168.         printf( "Changed\n" );
  169.     }
  170.     else
  171.       printf ("\n");
  172.     }
  173.     heap_status( hstate );     /* Finish up with heap status */
  174. }
  175.  
  176. void heap_status( int heapstate )
  177. /* heap_status decodes the status returned by far heap
  178.    management functions _fheapwalk, _fheapset, or _fheapchk,
  179.    and displays appropriate text.
  180. */
  181. {
  182.     /* Note 8 - _HEAPBADPTR, _HEAPBADBEGIN, and _HEAPBADNODE
  183.        are heap error values unique to Microsoft C6.0a.
  184.      */
  185.     switch( heapstate )
  186.     {
  187.     case _HEAPOK:
  188.         printf( "Heap status is OK.\n" );
  189.         break;
  190.     case _HEAPEMPTY:
  191.         printf( "Heap status is empty.\n" );
  192.         break;
  193.     case _HEAPEND:
  194.         printf( "Heap status is at end of heap.\n" );
  195.         break;
  196.     case _HEAPBADPTR:
  197.         printf( "Heap status is bad heap pointer.\n" );
  198.         break;
  199.     case _HEAPBADBEGIN:
  200.         printf( "Heap status is bad start of heap.\n" );
  201.         break;
  202.     case _HEAPBADNODE:
  203.         printf( "Heap status is bad heap node.\n" );
  204.         break;
  205.     }
  206. }
  207.